<?php

namespace app\worker\controller;
use app\common\model\mysql\CustomerAddress;
use app\common\model\mysql\CustomerOweBucket;
use app\common\model\mysql\Order;
use app\common\model\mysql\OrderBackBucket;
use app\common\model\mysql\OrderBackTicket;
use app\common\model\mysql\OrderBackWater;
use app\common\model\mysql\OrderOweBarrelsTicket;
use app\common\model\mysql\OrderProductNormal;
use app\common\model\mysql\OrderProductReturn;
use app\common\model\mysql\OrderProductWater;
use app\common\model\mysql\OrderTask;
use app\common\model\mysql\OrderTicket;
use app\common\model\mysql\Product;
use app\common\model\mysql\Send;
use app\common\model\mysql\Ticket;
use app\common\model\mysql\Worker;
use think\facade\Db;

class Complete extends AuthBase
{
    /**
     * 配送订单完成配送
     * @return \think\response\Json
     */
    public function completeDelivery(){
        $orderId = $this->request->param('order_id');
        if(empty($orderId)){
            return show(config("status.error"),"缺少参数订单id");
        }
        $longitude = $this->request->param('longitude');
        if(empty($longitude)){
            return show(config("status.error"),"缺少参数经度");
        }
        $latitude = $this->request->param('latitude');
        if(empty($latitude)){
            return show(config("status.error"),"缺少参数纬度");
        }
        Db::startTrans(); // 开始事务
        try {
            $sendDb     = (new Send());
            $orderDb    = (new Order());
            $workerDb   = (new Worker());
            $ticketDb   = (new Ticket());
            $productDb  = (new Product());
            $orderTaskDb    = (new OrderTask());
            $orderTicketDb  = (new OrderTicket());
            $orderBackWaterDb   = (new OrderBackWater());
            $customerAddressDb  = (new CustomerAddress());
            $orderBackBucketDb  = (new OrderBackBucket());
            $orderBackTicketDb  = (new OrderBackTicket());
            $orderProductWaterDb    = (new OrderProductWater());
            $customerOweBucketDb    = (new CustomerOweBucket());
            $orderProductReturnDb   = (new OrderProductReturn());
            $orderProductNormalDb   = (new OrderProductNormal());
            $orderOweBarrelsTicketDb= (new OrderOweBarrelsTicket());
            $field = "customer_id,customer_phone,customer_address,paytypeenum,stateenum,order_sn,send_id,sourceenum,worker_id_product_out,worker_name_product_out,customer_address_id,iscancel";
            $orderData = $orderDb->getOrderFind([['order_id','=',$orderId]],$field);
            if($orderData['send_id'] == 0){
                return show(config("status.error"),"该笔订单请先出厂审核");
            }
            if($orderData['worker_id_product_out'] == 0){
                return show(config("status.error"),"该笔订单未出厂审核，请先审核");
            }
            if($orderData['iscancel'] == 1){
                return show(config("status.error"),"该笔订单已取消，不允许完成配送");
            }
            if($orderData['stateenum'] == 64){
                return show(config("status.error"),"该笔订单未出厂，请库管审核出厂");
            }

            //stateenum:1.装配(未派工)，2(已派工)待配送 4(配送中)已出厂 8(已配送)已送完 16(库管处理完成)已回厂 32(已结账)财务已处理
            $sendData = $sendDb->getSendFind([['send_id','=',$orderData['send_id']]]);
            if($sendData['stateenum'] == 1 || $sendData['stateenum'] == 2){
                return show(config("status.error"),"该笔订单下配送单未出厂，请库管审核出厂");
            }
            if($sendData['stateenum'] == 32){
                return show(config("status.error"),"该笔订单下配送单收银已完成，请刷新页面");
            }
            $isPassStatus = 2;
            if($sendData['stateenum'] == 16){
                $isPassStatus = 1;
            }

            //回商品数据:order_product_normal_id,sellcount,sellcount_real
            $getProductData = $this->request->param('getProductData');
            if($getProductData){
                if($orderData['stateenum'] == 16){
                    foreach ($getProductData as $value){
                        $orderProductNormalData = $orderProductNormalDb->getOrderProductNormalFind([['order_product_normal_id','=',$value['order_product_normal_id']]],'sellcount_real');
                        if($orderProductNormalData['sellcount_real'] != $value['sellcount_real']){
                            return show(config("status.error"),"该笔订单已入厂，不能修改回商品数量");
                        }
                    }
                }
            }

            if($orderData['stateenum'] == 8 || $orderData['stateenum'] == 16){
                if($orderData['stateenum'] == 16){
                    $isPassStatus = 1;
                }
                $orderBackBucketDb->deleteAllNormalBackBucket([['order_id','=',$orderId]]);
                $orderBackTicketDb->deleteAllNormalBackTicket([['order_id','=',$orderId]]);
                $orderBackWaterDb->deleteAllNormalBackWater([['order_id','=',$orderId]]);
                $orderOweBarrelsTicketDb->deleteAllNormalOweBarrelsTicket([['order_id','=',$orderId]]);
            }

            //实收金额
            $payPrice = $this->request->param('payPrice',0);
            // 使用正则表达式验证支付金额是否为合法的数字（可选负号，整数部分，最多两位小数）
            if (!preg_match("/^[-]?\d+(\.\d{1,2})?$/", $payPrice)) {
                return show(config("status.error"),"实收金额输入错误");
            } else {
                // 如果验证通过，将支付金额四舍五入到两位小数
                $payPrice = round($payPrice, 2);
            }
            //破损数据：order_product_water_id,product_id,count
            $brokenData = $this->request->param('brokenData');
            //订单任务:order_id,order_task_id,taskstateenum
            $taskData = $this->request->param('taskData');
            //实送水数据:order_product_water_id,product_id,count_should,count_real,paytypeenum
            $getGoodsData = $this->request->param('getGoodsData');
            $getGoodsData = [
                ['order_product_water_id'=>1,'product_id'=>1,'count_should'=>2,'count_real'=>2,'paytypeenum'=>1],
                ['order_product_water_id'=>2,'product_id'=>2,'count_should'=>1,'count_real'=>1,'paytypeenum'=>1],
            ];
            //回桶数据:order_product_water_id,product_id,count,back_buckettypeenum
            $bucketData = $this->request->param('bucketData');
            $bucketData = [
                ['order_product_water_id'=>1,'product_id'=>3,'count'=>2,'back_buckettypeenum'=>1],
                ['order_product_water_id'=>2,'product_id'=>4,'count'=>1,'back_buckettypeenum'=>1]
            ];
            $orderProductWaterData = $orderProductWaterDb->getNormalOrderProductWater([['order_id','=',$orderId],['selltypeenum','=',1]]);
            if($orderProductWaterData){
                if(empty($getGoodsData)){
                    return show(config("status.error"),"缺少参数实送水数据");
                }
                if(empty($bucketData)){
                    return show(config("status.error"),"缺少参数回桶数据");
                }
            }
            //新增配送订单回水（破损）数据
            if($brokenData){
                foreach ($brokenData as $key => $value) {
                    $productData = $productDb->getProductFind([['product_id','=',$value['product_id']]],'product_id,product_name,price_tobad');
                    $orderOweBarrelsTicketData[$key]['order_id'] = $orderId;
                    $orderOweBarrelsTicketData[$key]['product_id'] = $value['product_id'];
                    $orderOweBarrelsTicketData[$key]['product_name'] = $productData['product_name'];
                    $orderOweBarrelsTicketData[$key]['worker_id'] = $this->workerId;
                    $orderOweBarrelsTicketData[$key]['worker_name'] = $this->workerName;
                    $orderOweBarrelsTicketData[$key]['adddate'] = date("Y-m-d H:i:s",time());
                    $orderOweBarrelsTicketData[$key]['tcount'] = $value['count'];
                    $orderOweBarrelsTicketData[$key]['back_watertypeenum'] = 1;
                    $orderOweBarrelsTicketData[$key]['tmoney'] = $productData['price_tobad'];
                    $orderOweBarrelsTicketData[$key]['order_product_water_id'] = $value['order_product_water_id'];
                }
                $orderBackWaterDb->addAllNormalBackWater($orderOweBarrelsTicketData);
            }
            //任务订单完成
            if($taskData){
                foreach ($taskData as $value) {
                    $orderTaskSaveData['taskstateenum'] = $value['taskstateenum'];
                    $orderTaskDb->saveOrderTaskData($value['order_task_id'],$orderTaskSaveData);
                }
            }
            //退桶数据:product_id,count
            $returnTotalPrice32 = 0;
            $backBuctetRefundData = $this->request->param('backBuctetRefundData');
            if($backBuctetRefundData){
                foreach ($backBuctetRefundData as $key => $value) {
                    $field = "product_id,parentid,product_name,typeenum,price_unit,bucket_price_pledge as price";
                    $productData = $productDb->getProductFind([['product_id','=',$value['product_id']]],$field);
                    if($productData['typeenum'] == 16){//杂桶价格
                        $productData['price'] = $productData['price_unit'];
                    }
                    $customerOweBucketWhere = [['customer_id','=',$orderData['customer_id'],['product_id','=',$value['product_id']]]];
                    $customerOweBucketData = $customerOweBucketDb->getCustomerOweBucketFind($customerOweBucketWhere,'bucket_owe_count,bucket_pledge_count,bucket_pledge_totalprice');//记录客户欠桶、压桶个数
                    if(empty($customerOweBucketData)){
                        $customerOweBucketData['bucket_pledge_count'] = $customerOweBucketData['bucket_owe_count'] = 0;
                    }
                    $RefundBucket[$key]['order_id'] = $orderId;
                    $RefundBucket[$key]['product_id'] = $value['product_id'];
                    $RefundBucket[$key]['product_name'] = $productData['product_name'];
                    $RefundBucket[$key]['product_id_water'] = $productData['parentid'];
                    $RefundBucket[$key]['tcount'] = $value['count'];
                    $RefundBucket[$key]['bucket_value'] = $productData['price_unit'];
                    $RefundBucket[$key]['price_unit'] = $productData['price'];
                    $RefundBucket[$key]['pricetotal_real']  = $productData['price']*$value['count'];
                    $RefundBucket[$key]['pledge_before_count'] = $customerOweBucketData['bucket_pledge_count'];
                    $RefundBucket[$key]['pledge_after_count']  = $customerOweBucketData['bucket_pledge_count']-$value['count'];
                    $RefundBucket[$key]['worker_id'] = $this->workerId;
                    $RefundBucket[$key]['worker_name'] = $this->workerName;
                    $RefundBucket[$key]['back_buckettypeenum'] = 32;
                    $RefundBucket[$key]['producttypeenum']  = $productData['typeenum'];
                    $RefundBucket[$key]['difference'] = $productData['price']*$value['count']*-1;
                    $RefundBucket[$key]['product_id_back'] = $productData['parentid'];
                    $returnTotalPrice32 += $productData['price']*$value['count'];
                }
                $orderBackBucketDb->addAllBackBucket($RefundBucket);
            }
            //多回桶数据:product_id,count
            $moreBarrelsBack = $this->request->param('moreBarrelsBack');
            if($moreBarrelsBack){
                foreach ($moreBarrelsBack as $key => $value) {
                    $field = "product_id,parentid,product_name,typeenum,price_unit,bucket_price_pledge as price";
                    $productData = $productDb->getProductFind([['product_id','=',$value['product_id']]],$field);
                    if($productData['typeenum'] == 16){//杂桶价格
                        $productData['price'] = $productData['price_unit'];
                    }
                    $dataBack[$key]['order_id'] = $orderId;
                    $dataBack[$key]['product_id'] = $value['product_id'];
                    $dataBack[$key]['product_name'] = $productData['product_name'];
                    $dataBack[$key]['product_id_water'] = $productData['parentid'];
                    $dataBack[$key]['tcount'] = $value['count'];
                    $dataBack[$key]['bucket_value'] = $productData['price_unit'];
                    $dataBack[$key]['price_unit'] = $productData['price'];
                    $dataBack[$key]['worker_id'] = $this->workerId;
                    $dataBack[$key]['worker_name'] = $this->workerName;
                    $dataBack[$key]['back_buckettypeenum'] = 512;
                    $dataBack[$key]['producttypeenum']  = $productData['typeenum'];
                    $dataBack[$key]['product_id_back'] = $productData['parentid'];
                }
                $orderBackBucketDb->addAllBackBucket($dataBack);
            }
            //多回票数据:ticket_id,count
            $moreTicketBack = $this->request->param('moreTicketBack');
            if($moreTicketBack){
                foreach ($moreTicketBack as $key => $value) {
                    $field = "id as ticket_id,tname as ticket_name,product_code,price_unit";
                    $ticketData = $ticketDb->getTicketFind([['id','=',$value['ticket_id']]],$field);
                    $productData = $productDb->getProductFind([['shop_id','=',$this->shopId],['tcode','=',$ticketData['product_code']],['isdel','=','0']],'product_id,product_name');
                    $tataBack[$key]['order_id'] = $orderId;
                    $tataBack[$key]['product_id'] = $productData['product_id'];
                    $tataBack[$key]['ticket_id'] = $ticketData['ticket_id'];
                    $tataBack[$key]['ticket_name'] = $ticketData['ticket_name'];
                    $tataBack[$key]['ticket_count'] = $value['count'];
                    $tataBack[$key]['ticket_value'] = $ticketData['price_unit']*$value['count'];
                    $tataBack[$key]['back_tickettypeenum'] = 64;
                }
                $orderBackTicketDb->addAllBackTicket($tataBack);
            }
            //回桶
            if($bucketData){
                foreach ($bucketData as $key=>$value){
                    $field = "product_id,parentid,product_name,typeenum,price_unit,bucket_price_pledge as price";
                    $productData = $productDb->getProductFind([['product_id','=',$value['product_id']]],$field);
                    if($productData['typeenum'] == 16){//杂桶价格
                        $productData['price'] = $productData['price_unit'];
                        $productData['parentid'] = 0;
                    }
                    $customerOweBucketWhere = [['customer_id','=',$orderData['customer_id'],['product_id','=',$value['product_id']]]];
                    $customerOweBucketData = $customerOweBucketDb->getCustomerOweBucketFind($customerOweBucketWhere,'bucket_owe_count,bucket_pledge_count,bucket_pledge_totalprice');//记录客户欠桶、压桶个数
                    if(empty($customerOweBucketData)){
                        $customerOweBucketData['bucket_pledge_count'] = $customerOweBucketData['bucket_owe_count'] = 0;
                    }
                    $orderProductWaterData = $orderProductWaterDb->getOrderProductWaterFind([['order_product_water_id','=',$value['order_product_water_id']]],'product_id,paytypeenum');
                    $backBucketData[$key]['order_id'] = $orderId;
                    $backBucketData[$key]['product_id'] = $value['product_id'];
                    $backBucketData[$key]['product_name'] = $productData['product_name'];
                    $backBucketData[$key]['product_id_water'] = $productData['parentid'];
                    $backBucketData[$key]['tcount'] = $value['count'];
                    $backBucketData[$key]['bucket_value'] = $productData['price_unit'];
                    $backBucketData[$key]['price_unit'] = $productData['price'];
                    $backBucketData[$key]['pricetotal_real']  = $productData['price']*$value['count'];
                    $backBucketData[$key]['pledge_before_count'] = $customerOweBucketData['bucket_pledge_count'];
                    $backBucketData[$key]['pledge_after_count']  = $customerOweBucketData['bucket_pledge_count']-$value['count'];
                    $backBucketData[$key]['owe_before_count'] = $customerOweBucketData['bucket_owe_count'];
                    $backBucketData[$key]['owe_after_count']  = $customerOweBucketData['bucket_owe_count']-$value['count'];
                    $backBucketData[$key]['worker_id'] = $this->workerId;
                    $backBucketData[$key]['worker_name'] = $this->workerName;
                    $backBucketData[$key]['back_buckettypeenum'] = $value['back_buckettypeenum'];
                    $backBucketData[$key]['producttypeenum']  = $productData['typeenum'];
                    $backBucketData[$key]['difference'] = $productData['price']*$value['count'];
                    $backBucketData[$key]['product_id_back'] = $orderProductWaterData['product_id'];
                    $backBucketData[$key]['order_product_water_id'] = $value['order_product_water_id'];
                    if($value['back_buckettypeenum'] == 1){//回桶
                        $backBucketData[$key]['pricetotal_real']  = 0;
                        $backBucketData[$key]['pledge_before_count']  = 0;
                        $backBucketData[$key]['pledge_after_count']  = 0;
                        $backBucketData[$key]['owe_before_count'] = 0;
                        $backBucketData[$key]['owe_after_count']  = 0;
                        $backBucketData[$key]['difference'] = 0;
                    }elseif ($value['back_buckettypeenum'] == 2){//押桶
                        $backBucketData[$key]['pledge_after_count']  = $customerOweBucketData['bucket_pledge_count']+$value['count'];
                        $backBucketData[$key]['owe_before_count'] = 0;
                        $backBucketData[$key]['owe_after_count']  = 0;
                    }elseif($value['type'] == 4){//还欠桶
                        $backBucketData[$key]['pricetotal_real']  = 0;
                        $backBucketData[$key]['pledge_before_count']  = 0;
                        $backBucketData[$key]['pledge_after_count']  = 0;
                    }elseif($value['type'] == 8){//欠桶转押桶
                        $backBucketData[$key]['pledge_after_count']  = $customerOweBucketData['bucket_pledge_count']+$value['count'];
                    }elseif($value['type'] == 16){//欠桶转卖桶
                        $backBucketData[$key]['price_unit']       = $productData['price_unit'];
                        $backBucketData[$key]['pricetotal_real']  = $productData['price_unit']*$value['count'];
                        $backBucketData[$key]['pledge_after_count']  = $customerOweBucketData['bucket_pledge_count']+$value['count'];
                        $backBucketData[$key]['difference'] = $productData['price_unit']*$value['count'];
                    }elseif($value['type'] == 32){//退桶
                        $backBucketData[$key]['owe_before_count'] = 0;
                        $backBucketData[$key]['owe_after_count']  = 0;
                        $backBucketData[$key]['difference'] = $productData['price']*$value['count']*-1;
                    }elseif($value['type'] == 64){//欠桶
                        $backBucketData[$key]['price_unit']       = $productData['price_unit'];
                        $backBucketData[$key]['pricetotal_real']  = $productData['price_unit']*$value['count'];
                        $backBucketData[$key]['pledge_before_count'] = 0;
                        $backBucketData[$key]['pledge_after_count']  = 0;
                        $backBucketData[$key]['owe_after_count']  = $customerOweBucketData['bucket_owe_count']+$value['count'];
                        $backBucketData[$key]['difference'] = 0;
                        //记录欠桶数据
                        $oweData['customer_id'] = $orderData['customer_id'];
                        $oweData['order_id'] = $orderId;
                        $oweData['order_sn'] = $orderData['order_sn'];
                        $oweData['owe_type'] = 1;
                        $oweData['owe_count'] = $value['count'];
                        $oweData['product_id'] = $value['product_id'];
                        $oweData['tname'] = $productData['product_name'];
                        $oweData['address'] = $orderData['customer_address'];
                        $oweData['adddate'] = date("Y-m-d H:i:s",time());
                        $oweData['worker_id_add'] = $oweData['wo_workerid_add'] = $this->workerId;
                        $oweData['worker_name_add'] = $oweData['wo_workername_add'] = $this->workerName;
                        $oweData['company_id'] = $this->companyId;
                        $oweData['shop_id'] = $this->shopId;
                        $oweData['phone'] = $orderData['customer_phone'];
                        $oweData['count_initial'] = $value['count'];
                        $oweData['or_order_product_waterid'] = $value['order_product_water_id'];
                        $oweData['paytypeenum'] = $orderProductWaterData['paytypeenum'];
                        $orderOweBarrelsTicketDb->addNormalOweBarrelsTicket($oweData);
                    }elseif($value['type'] == 128){//卖桶
                        $backBucketData[$key]['price_unit']       = $productData['price_unit'];
                        $backBucketData[$key]['pricetotal_real']  = $productData['price_unit']*$value['count'];
                        $backBucketData[$key]['pledge_before_count'] = 0;
                        $backBucketData[$key]['pledge_after_count']  = 0;
                        $backBucketData[$key]['owe_before_count'] = 0;
                        $backBucketData[$key]['owe_after_count']  = 0;
                        $backBucketData[$key]['difference'] = $productData['price_unit']*$value['count'];
                    }elseif($value['type'] == 256){//不正常回桶
                        $backBucketData[$key]['pricetotal_real']  = 0;
                        $backBucketData[$key]['pledge_before_count'] = 0;
                        $backBucketData[$key]['pledge_after_count']  = 0;
                        $backBucketData[$key]['owe_before_count'] = 0;
                        $backBucketData[$key]['owe_after_count']  = 0;
                    }else{
                        return show(config("status.error"),"回桶类型不正确");
                    }
                }
                $orderBackBucketDb->addAllBackBucket($backBucketData);
            }
            //实送水
            $isUseTicketBuy = 0;//是否使用水票购买
            $price_month = $water_should = $price_free = $giving_free = 0;
            if($getGoodsData){
                foreach ($getGoodsData as $value){
                    $saveData['bucketback_count_should'] = $saveData['bucketback_count_real'] = $value['count_real'];
                    $orderWaterWhere = [
                        ['order_id','=',$orderId],
                        ['product_id','=',$value['product_id']],
                        ['order_product_water_id','=',$value['order_product_water_id']]
                    ];
                    $orderWaterData = $orderProductWaterDb->getOrderProductWaterFind($orderWaterWhere,'selltypeenum,paytypeenum,bucket_value,sellcount_real,isdifference,ticket_value,tk_ticketcount_real,p_productid_bucket,tk_ticketcount_should,price_diff_ticket_self');
                    if($orderWaterData['selltypeenum'] == 2){
                        $saveData['tk_ticketcount_should'] = $saveData['tk_ticketcount_real'] = 0;
                    }else{
                        if($value['paytypeenum'] == 1){
                            $saveData['tk_ticketcount_should'] = $saveData['tk_ticketcount_real'] = $value['count_real'];
                            $isUseTicketBuy = 1;
                        }else{
                            $saveData['tk_ticketcount_should'] = $saveData['tk_ticketcount_real'] = 0;
                        }
                    }
                    $saveData['sellcount_real'] = $value['count_real'];
                    $saveData['paytypeenum'] = $value['paytypeenum'];
                    $orderProductWaterDb->saveOrderProductWaterData($orderWaterWhere,$saveData);

                }
            }
            if($isUseTicketBuy == 1){
                //回票数据:order_product_water_id,ticket_id,back_tickettypeenum,count,count_should
                $ticketData = $this->request->param('ticketData');
                $ticketData = [
                    ['order_product_water_id'=>1,'ticket_id'=>1,'back_tickettypeenum'=>1,'count'=>2,'count_should'=>2],
                    ['order_product_water_id'=>2,'ticket_id'=>2,'back_tickettypeenum'=>1,'count'=>1,'count_should'=>1]
                ];
                if(empty($ticketData)){
                    return show(config("status.error"),"缺少参数回票数据");
                }
                $array = 0;
                $ticketDatas = []; // 确保 $ticketDatas 被初始化
                foreach ($ticketData as $value) {
                    $orderWaterWhere = [
                        ['order_product_water_id','=',$value['order_product_water_id']]
                    ];
                    $orderWaterData = $orderProductWaterDb->getOrderProductWaterFind($orderWaterWhere,'selltypeenum,paytypeenum,product_id');
                    // 只有当 paytypeenum 为 1 且 ticket_id 存在时才进行处理
                    if ($orderWaterData['paytypeenum'] == 1 && !empty($value['ticket_id'])) {
                        $ticketDatas[$array] = [
                            'order_product_water_id'=> $value['order_product_water_id'],
                            'ticket_id'             => $value['ticket_id'],
                            'back_tickettypeenum'   => $value['back_tickettypeenum'],
                            'count'                 => $value['count'],
                            'count_should'          => $value['count_should'],
                            'product_id'            => $orderWaterData['product_id'],
                            'selltypeenum'          => $orderWaterData['selltypeenum'],
                            'paytypeenum'           => $orderWaterData['paytypeenum'],
                        ];
                        $array++;
                    }
                }
                foreach ($ticketDatas as $key=>$value){
                    if($value['selltypeenum'] == 1){
                        $ticketFindData = $ticketDb->getTicketFind([['id','=',$value['ticket_id']]],'id as ticket_id,tname as ticket_name,price_unit,product_code');
                        // 初始化回票数据
                        $backTicketData = [
                            'order_id' => $orderId,
                            'product_id' => $value['product_id'],
                            'order_product_water_id' => $value['order_product_water_id'],
                            'ticket_id' => $value['ticket_id'],
                            'ticket_name' => $ticketFindData['ticket_name'],
                            'ticket_count_should' => $value['count_should'],
                            'ticket_count' => $value['count'],
                            'ticket_value' => $ticketFindData['price_unit'] * $value['count'],
                            'back_tickettypeenum' => $value['back_tickettypeenum'],
                            'difference' => 0,
                            'order_product_normal_id' => 0,
                            'product_id_back' => $value['product_id'],
                            'diff_price_should' => 0,
                            'diff_price_real' => 0,
                        ];
                        //回票类型，1回票 2欠票 4还欠票  8卖水(卖票) 16欠转卖 32 不正常回票
                        switch ($value['back_tickettypeenum']) {
                            case 1:
                                // 类型1的逻辑（如果有的话）
                                break;
                            case 2:
                                // 记录欠票数据
                                $oweData = [
                                    'customer_id' => $orderData['customer_id'],
                                    'order_id' => $orderId,
                                    'order_sn' => $orderData['order_sn'],
                                    'owe_type' => 2,
                                    'owe_count' => $value['count'],
                                    'product_id' => $value['product_id'],
                                    'tname' => $ticketFindData['ticket_name'],
                                    'address' => $orderData['customer_address'],
                                    'adddate' => date("Y-m-d H:i:s"),
                                    'worker_id_add' => $this->workerId,
                                    'worker_name_add' => $this->workerName,
                                    'wo_workerid_add' => $this->workerId,
                                    'wo_workername_add' => $this->workerName,
                                    'company_id' => $this->companyId,
                                    'shop_id' => $this->shopId,
                                    'phone' => $orderData['customer_phone'],
                                    'count_initial' => $value['count'],
                                    'or_order_product_waterid' => $value['order_product_water_id'],
                                    'paytypeenum' => $value['paytypeenum'],
                                ];
                                $orderOweBarrelsTicketDb->addNormalOweBarrelsTicket($oweData);
                                break;
                            case 4:
                                // 类型4的逻辑（如果有的话）
                                break;
                            case 8:
                            case 16:
                            case 32:
                                $backTicketData['difference'] = $ticketFindData['price_unit'] * $value['count'];
                                break;
                        }
                        // 添加到回票数据数组
                        $backTicketDataArray[$key] = $backTicketData;
                    }
                }
                $orderBackTicketDb->addAllBackTicket($backTicketDataArray);
            }
            $orderProductWaterSellData = $orderProductWaterDb->getNormalOrderProductWater([['order_id','=',$orderId]]);

            if($orderProductWaterSellData){
                foreach ($orderProductWaterSellData as $value){
                    $bucketCountReal = $orderBackBucketDb->getNormalBackBucketSum([['order_product_water_id','=',$value['order_product_water_id']], ['back_buckettypeenum','in',[1,256]]],'tcount');
                    $ticketCountReal = $orderBackTicketDb->getNormalBackTicketSum([['order_product_water_id','=',$value['order_product_water_id']],['back_tickettypeenum','in',[1,32]]],'ticket_count');
                    $ticketCountShould = $orderBackTicketDb->getNormalBackTicketSum([['order_product_water_id','=',$value['order_product_water_id']],['back_tickettypeenum','=',8]],'ticket_count');
                    $ticketCountShould = max(0, $value['tk_ticketcount_should'] - $ticketCountShould);
                    // 准备更新数据
                    $realCount = [
                        'tk_ticketcount_should' => $ticketCountShould,
                        'bucketback_count_real' => max(0, $bucketCountReal),
                        'tk_ticketcount_real' => max(0, $ticketCountReal)
                    ];
                    $orderProductWaterDb->saveOrderProductWaterData([['order_product_water_id','=',$value['order_product_water_id']]],$realCount);

                    if ($value['selltypeenum'] == 1) {
                        if($value['paytypeenum'] == 1){
                            $ticketPriceReal = $orderBackTicketDb->getNormalBackTicketSum([['order_product_water_id','=',$value['order_product_water_id']],['back_tickettypeenum','=',8]],'ticket_value');
                            $saveData = ['price_should' => 0, 'price_real' => $ticketPriceReal ?: 0];
                            $orderProductWaterDb->saveOrderProductWaterData([['order_product_water_id','=',$value['order_product_water_id']]],$saveData);
                        }
                        if ($value['paytypeenum'] == 4) {
                            $price = $value['price_unit'] * $value['sellcount_real'];
                            $saveData = ['price_should' => $price, 'price_real' => $price];
                            $water_should += $price;
                            $orderProductWaterDb->saveOrderProductWaterData([['order_product_water_id','=',$value['order_product_water_id']]],$saveData);
                        }
                        if ($value['paytypeenum'] == 8) {
                            $price_month += $value['price_unit'] * $value['sellcount_real'];
                        }
                        if ($value['paytypeenum'] == 16) {
                            $price_free += $value['price_unit'] * $value['sellcount_real'];
                        }
                    }elseif($value['selltypeenum'] == 2) {
                        $giving_free += $value['price_unit'] * $value['sellcount_real'];
                    }
                }
            }
            $diffprice_bucket = $price_diff_ticket = $totalPrice2 = $totalPrice16 = $totalPrice8 = $totalPrice32 = $totalPrice128 = $ticketTotalPricess = $passDiff = 0;
            if($orderProductWaterData){
                $yatongBack = 2;
                $buzhengchangBack = 2;
                foreach ($getGoodsData as $value){
                    $ticketOrderPrice = $ticketOrderPriceBuCha = 0;
                    $orderWaterWhere = [
                        ['order_id','=',$orderId],
                        ['product_id','=',$value['product_id']],
                        ['order_product_water_id','=',$value['order_product_water_id']]
                    ];
                    $orderWaterData = $orderProductWaterDb->getOrderProductWaterFind($orderWaterWhere,'selltypeenum,paytypeenum,bucket_value,sellcount_real,isdifference,ticket_value,tk_ticketcount_real,p_productid_bucket,tk_ticketcount_should,price_diff_ticket_self,order_product_water_id');
                    $productBucketData = $productDb->getProductFind([['product_id','=',$orderWaterData['p_productid_bucket']]],'bucket_price_pledge');

                    $backBucketDatas = $orderBackBucketDb->getNormalBackBucket([['order_product_water_id','=',$orderWaterData['order_product_water_id']]]);
                    $bucketCount = $orderBackBucketDb->getNormalBackBucketSum([['order_product_water_id','=',$orderWaterData['order_product_water_id']],['back_buckettypeenum','in',[2,64,128]]],'tcount');
                    $totalcountBackReal = 1;
                    if($orderWaterData['sellcount_real'] == $bucketCount){
                        $totalcountBackReal = 0;
                    }
                    $bucketPricePledge = $productBucketData['bucket_price_pledge'];//桶的押桶价
                    $bucketOrderPrice = $bucketPricePledge*($orderWaterData['sellcount_real']-$bucketCount);
                    $bucketOrderPriceBuCha = $bucketPricePledge*($orderWaterData['sellcount_real']-$bucketCount);
                    //回桶类型,1回桶 2押桶 4还欠桶 8欠桶转押桶 16 欠桶转卖桶 32 退桶 64 欠桶 128 卖桶 256 不正常回桶
                    foreach ($backBucketDatas as $v) {
                        if($v['back_buckettypeenum'] == 1){
                            $bucketOrderPrice = $bucketOrderPrice-$v['price_unit']*$v['tcount'];
                            $bucketOrderPriceBuCha = $bucketOrderPriceBuCha-$v['price_unit']*$v['tcount'];
                        }elseif ($v['back_buckettypeenum'] == 2) {
                            $yatongBack = 1;
                            $bucketOrderPrice = $bucketOrderPrice+$v['price_unit']*$v['tcount'];
                            $bucketOrderPriceBuCha = $bucketOrderPriceBuCha+$v['price_unit']*$v['tcount'];
                            $totalPrice2 += $v['price_unit']*$v['tcount'];
                        }elseif ($v['back_buckettypeenum'] == 8) {
                            $bucketOrderPrice = $bucketOrderPrice+$v['price_unit']*$v['tcount'];
                            $totalPrice8 += $v['price_unit']*$v['tcount'];
                        }elseif ($v['back_buckettypeenum'] == 16) {
                            $bucketOrderPrice = $bucketOrderPrice+$v['bucket_value']*$v['tcount'];
                            $totalPrice16 += $v['bucket_value']*$v['tcount'];
                        }elseif ($v['back_buckettypeenum'] == 32) {
                            $totalPrice32 += $v['price_unit']*$v['tcount'];
                        }elseif ($v['back_buckettypeenum'] == 128) {
                            $bucketOrderPrice = $bucketOrderPrice+$v['bucket_value']*$v['tcount'];
                            $totalPrice128 += $v['bucket_value']*$v['tcount'];
                        }elseif ($v['back_buckettypeenum'] == 256) {
                            if($totalcountBackReal){//押桶+卖桶+欠桶 !== 实送数
                                if($orderWaterData['isdifference'] == 2){
                                    $buzhengchangBack = 1;
                                    $bucketOrderPrice = $bucketOrderPrice-$bucketPricePledge*$v['tcount'];
                                    $bucketOrderPriceBuCha = $bucketOrderPriceBuCha-$bucketPricePledge*$v['tcount'];
                                }else{
                                    if($bucketPricePledge<$v['price_unit']){
                                        $bucketOrderPrice = $bucketOrderPrice-$bucketPricePledge*$v['tcount'];
                                        $bucketOrderPriceBuCha = $bucketOrderPriceBuCha-$bucketPricePledge*$v['tcount'];
                                    }else{
                                        $bucketOrderPrice = $bucketOrderPrice-$v['price_unit']*$v['tcount'];
                                        $bucketOrderPriceBuCha = $bucketOrderPriceBuCha-$v['price_unit']*$v['tcount'];
                                    }
                                }
                            }
                        }
                        if ($buzhengchangBack == 1 && $yatongBack == 2) {
                            $bucketOrderPrice = 0;
                            $bucketOrderPriceBuCha = 0;
                        }
                    }

                    if($orderWaterData['paytypeenum'] == 1){
                        $backTicketDatas = $orderBackTicketDb->getNormalBackTicket([['order_product_water_id','=',$orderWaterData['order_product_water_id']],['order_product_normal_id','=',0]]);

                        $ticketCount = $orderBackTicketDb->getNormalBackTicketSum([['order_product_water_id','=',$orderWaterData['order_product_water_id']],['back_tickettypeenum','in',[2,8]]],'ticket_count');

                        $ticket_value = $orderWaterData['ticket_value'];
                        $ticketOrderPrice = $ticket_value*($orderWaterData['sellcount_real']-$ticketCount);
                        $ticketOrderPriceBuCha = $ticket_value*($orderWaterData['sellcount_real']-$ticketCount);
                        //回票类型，1回票 2欠票 4还欠票  8卖水(卖票) 16欠转卖 32 不正常回票
                        $SellingTickets = 0;
                        $passBackTicketDifference = 2;
                        foreach ($backTicketDatas as $va) {
                            $yuanTicketMoney = $orderProductWaterDb->getOrderProductWaterFind([['order_product_water_id','=',$va['order_product_water_id']]],'ticket_value');
                            $yuanTicketMoney = $yuanTicketMoney['ticket_value'];
                            if($va['back_tickettypeenum'] == 1){
                                $ticketOrderPrice = $ticketOrderPrice-$va['ticket_value'];
                                $ticketOrderPriceBuCha = $ticketOrderPriceBuCha-$va['ticket_value'];
                            }elseif ($va['back_tickettypeenum'] == 8) {
                                $ticketOrderPrice = $ticketOrderPrice+$va['ticket_value'];
                                $SellingTickets += $va['ticket_value'];
                            }elseif ($va['back_tickettypeenum'] == 16) {
                                $ticketOrderPrice = $ticketOrderPrice+$va['ticket_value'];
                                $ticketTotalPricess += $va['ticket_value'];
                            }elseif ($va['back_tickettypeenum'] == 32) {
                                $passBackTicketDifference = 1;
                                if($orderWaterData['price_diff_ticket_self'] == "-1"){
                                    if($yuanTicketMoney['']<$va['ticket_value']/$va['ticket_count']){
                                        $ticketOrderPrice = $ticketOrderPrice-$yuanTicketMoney*$va['ticket_count'];
                                        $ticketOrderPriceBuCha = $ticketOrderPriceBuCha-$yuanTicketMoney*$va['ticket_count'];
                                    }else{
                                        $ticketOrderPrice = $ticketOrderPrice-$va['ticket_value'];
                                        $ticketOrderPriceBuCha = $ticketOrderPriceBuCha-$va['ticket_value'];
                                    }
                                }else{
                                    $passDiff = $orderWaterData['price_diff_ticket_self'];
                                    $ticketOrderPrice = $orderWaterData['price_diff_ticket_self'];
                                    $ticketOrderPriceBuCha = $orderWaterData['price_diff_ticket_self'];
                                }
                            }
                            if($passBackTicketDifference == 1){
                                if($orderWaterData['isdifference'] == 2){
                                    if($SellingTickets>0){
                                        $ticketOrderPrice = $SellingTickets;
                                        $ticketOrderPriceBuCha = $SellingTickets;
                                    }else{
                                        $ticketOrderPrice = 0;
                                        $ticketOrderPriceBuCha = 0;
                                    }
                                }
                            }
                        }
                    }

                    $diffprice_bucket += $bucketOrderPrice;
                    $price_diff_ticket += $ticketOrderPrice;
                    $ticketOrderPriceBuCha = max(0, $ticketOrderPriceBuCha);
                    $bucketOrderPriceBuCha = max(0, $bucketOrderPriceBuCha);
                    $saveOrderWaterData['price_diff_ticket'] = $ticketOrderPriceBuCha;
                    $saveOrderWaterData['price_diff_bucket'] = $bucketOrderPriceBuCha;
                    $orderProductWaterDb->saveOrderProductWaterData([['order_product_water_id','=',$orderWaterData['order_product_water_id']]],$saveOrderWaterData);
                }
            }
            //购票数据://order_ticket_id,count_should,count_real
            $buyTicketData = $this->request->param('buyTicketData');
            if($buyTicketData){
                foreach ($buyTicketData as $value) {
                    if($value['count_should'] > $value['count_real']){
                        $orderTicketSaveData['count_real'] = 0;
                    }else{
                        $orderTicketData = $orderTicketDb->getOrderTicketFind([['order_ticket_id','=',$value['order_ticket_id']]],'count_give,count_cost');
                        $orderTicketSaveData['count_real'] = $orderTicketData['count_give']+$orderTicketData['count_cost'];
                    }
                    $orderTicketDb->saveOrderTicketData($value['order_ticket_id'],$orderTicketSaveData);
                }
            }
            //回商品数据:product_normal_id,count_should,count_real
            $getProductData = $this->request->param('getProductData');
            if($getProductData){
                foreach ($getProductData as $value) {
                    $orderProductNormalData = $orderProductNormalDb->getOrderProductNormalFind([['order_product_normal_id','=',$value['product_normal_id']]]);
                    $normalData['sellcount_real'] = $value['count_real'];
                    if(empty($value['count_real'])){
                        $normalData['price_selltotal'] = 0;
                    }else{
                        $normalData['price_selltotal'] = $orderProductNormalData['price_selltotal']/$orderProductNormalData['sellcount']*$value['count_real'];
                        if(empty($normalData['price_selltotal'])){
                            $normalData['price_selltotal'] = 0;
                        }
                    }
                    $orderProductNormalDb->saveOrderProductNormalData($value['product_normal_id'],$normalData);
                }
            }

            $oPNWhere = [['order_id','=',$orderId], ['selltypeenum','=',1], ['paytypeenum','in',[4,8,16]], ['sellcount_real','>',0]];
            $expression = "SUM(price_sellunit * sellcount_real) as total";
            $productPrice = $orderProductNormalDb->getOrderProductNormalSumTotal($oPNWhere,$expression);
            $oPNWhere = [['order_id','=',$orderId], ['selltypeenum','=',1], ['paytypeenum','=',8], ['sellcount_real','>',0]];
            $price_months = $orderProductNormalDb->getOrderProductNormalSumTotal($oPNWhere,$expression);
            $oPNWhere = [['order_id','=',$orderId], ['selltypeenum','=',1], ['paytypeenum','=',16], ['sellcount_real','>',0]];
            $price_frees = $orderProductNormalDb->getOrderProductNormalSumTotal($oPNWhere,$expression);
            $oPNWhere = [['order_id','=',$orderId], ['selltypeenum','=',2], ['sellcount_real','>',0]];
            $giving_frees = $orderProductNormalDb->getOrderProductNormalSumTotal($oPNWhere,$expression);
            $tickePrice = $orderTicketDb->getNormalOrderTicketSum([['order_id','=',$orderId], ['count_real','>',0]],'price_selltotal');
            //任务收欠款
            $taskWhere = [['order_id','=',$orderId], ['tasktypeenum','=',2], ['taskstateenum','=',2]];
            $taskPrice = $orderTaskDb->getNormalOrderTaskSum($taskWhere,'tmoney');
            //任务收月结
            $taskWhere = [['order_id','=',$orderId], ['tasktypeenum','=',3], ['taskstateenum','=',2]];
            $taskPrices = $orderTaskDb->getNormalOrderTaskSum($taskWhere,'tmoney');
            //任务退货补差价
            $taskWhere = [['order_id','=',$orderId], ['tasktypeenum','=',6], ['taskstateenum','=',2]];
            $taskPricess = $orderTaskDb->getNormalOrderTaskSum($taskWhere,'tmoney');
            //退货商品
            $productreturnData = $orderProductReturnDb->getNormalOrderProductReturn([['order_id','=',$orderId],['isdel','=','0']],"order_product_return_id,product_id,typeenum,price_diff");
            $productreturnPrice = 0;
            if($productreturnData){
                foreach ($productreturnData as $value) {
                    $productreturnPrice += $value['price_diff'];
                    if($value['typeenum'] == 1){
                        $badProductWhere = [
                            ['typeenum','=',64],
                            ['parentid','=',$value['product_id']]
                        ];
                    }elseif($value['typeenum'] == 2){
                        $badProductWhere = [
                            ['typeenum','=',32],
                            ['parentid','=',$value['product_id']]
                        ];
                    }
                    $badProductData = $productDb->getProductFind($badProductWhere,"product_id,product_name");
                    $badProductDatas['product_id_bad'] = $badProductData['product_id'];
                    $badProductDatas['product_name_bad'] = $badProductData['product_name'];
                    $orderProductReturnDb->saveOrderProductReturn($value['order_product_return_id'],$badProductDatas);
                }
            }
            //普通商品是否用水票购买
            $NormalWhere = [['order_id','=',$orderId], ['selltypeenum','=',1], ['paytypeenum','=',1]];
            $field = "order_product_normal_id,tk_ticketid,tk_ticket_count,product_id";
            $goodsTicketBuy = $orderProductNormalDb->getNormalOrderProductNormal($NormalWhere,$field);
            if($goodsTicketBuy){
                foreach ($goodsTicketBuy as $key => $value) {
                    $ticketData = $ticketDb->getTicketFind([['id','=',$value['tk_ticketid']]],'id as ticket_id,tname as ticket_name,price_unit,product_code');
                    $TicketBuy[$key]['ticket_value']     = $ticketData['price_unit']*$value['tk_ticket_count'];
                    $TicketBuy[$key]['difference']   = $ticketData['price_unit']*$value['tk_ticket_count'];
                    $TicketBuy[$key]['order_id']   = $orderId;
                    $TicketBuy[$key]['product_id']  = $value['product_id'];
                    $TicketBuy[$key]['ticket_id']      = $ticketData['ticket_id'];
                    $TicketBuy[$key]['ticket_name']    = $ticketData['ticket_name'];
                    $TicketBuy[$key]['ticket_count']   = $value['tk_ticket_count'];
                    $TicketBuy[$key]['back_tickettypeenum'] = 1;
                    $TicketBuy[$key]['ticket_count_should'] = $value['tk_ticket_count'];
                    $TicketBuy[$key]['order_product_normal_id'] = $value['order_product_normal_id'];
                }
                $orderBackTicketDb->addAllBackTicket($TicketBuy);
            }

            if($isPassStatus == 1){
                $orderP['stateenum'] = 16;
            }else{
                $orderP['stateenum'] = 8;
            }
            $orderP['price_account'] = 0;
            $orderP['price_wechat'] = 0;
            $orderP['completedate'] = date("Y-m-d H:i:s",time());
            $orderP['sendokdate'] = date("Y-m-d H:i:s",time());
            $orderP['issendstateenum'] = 2;
            $orderP['price_diff_bucket'] = ($bucketOrderPriceBuCha>0)?($bucketOrderPriceBuCha):0;//桶的总差价
            $orderP['price_diff_ticket'] = ($ticketOrderPriceBuCha>0)?($ticketOrderPriceBuCha):0;//水票的总差价
            if($passDiff){
                $orderP['price_diff_ticket'] = $passDiff;
            }
            $orderP['price_sell_water_should'] = $water_should+$SellingTickets;//现金售水的应收价格
            $orderP['price_sell_product_should'] = $productPrice;//现金售普通商品的应收价格
            $orderP['price_sell_ticket_should'] = $tickePrice;//售票应收
            $orderP['price_pledge_bucket'] = $totalPrice2;//押桶总价
            $orderP['price_owe_sell_bucket'] = $totalPrice16;//桶欠转卖总价
            $orderP['price_owe_sell_ticket'] = $ticketTotalPricess;//票欠转卖总价
            $orderP['price_owe_pledge_bucket'] = $totalPrice8;//桶欠转押总价
            $orderP['price_sell_bucket'] = $totalPrice128;//卖桶总价
            $orderP['price_bucket_return'] = ($totalPrice32*-1)+($returnTotalPrice32*-1);
            $orderP['price_owe_back'] = $taskPrice;//还欠款
            $orderP['price_return_diff'] = $taskPricess;//退货补差价
            $orderP['price_month'] = $price_month+$price_months;
            $orderP['monthly_statement'] = $taskPrices;
            $orderP['price_free'] = $price_free+$price_frees;
            $orderP['price_give'] = $giving_free+$giving_frees;
            $diffprice_bucket = ($diffprice_bucket>0)?($diffprice_bucket):0;
            $price_diff_ticket = ($price_diff_ticket>0)?($price_diff_ticket):0;
            if($passDiff){
                $price_diff_ticket = $passDiff;
            }
            $amounts = $diffprice_bucket+$price_diff_ticket+$orderP['price_sell_water_should']+$orderP['price_sell_product_should']+$orderP['price_sell_ticket_should']+$orderP['price_owe_sell_bucket']+$orderP['price_owe_sell_ticket']+$orderP['price_owe_pledge_bucket']+$taskPrice+$totalPrice128-$SellingTickets+$productreturnPrice+$orderP['price_month']+$orderP['price_free']+$taskPrices-$totalPrice32+$orderP['price_give']+$taskPricess+$orderP['price_account']+$orderP['price_wechat']-$returnTotalPrice32;//应收金额

            if($price_months>0){
                $amounts = $amounts-$price_months;
            }
            if($price_frees>0){
                $amounts = $amounts-$price_frees;
            }
            $orderP['amount'] = $amounts;//0-60 = -60

            $orderP['paid_in'] = $payPrice;//实收金额
            $orderP['arrears'] = $orderP['amount']-$orderP['paid_in']-$orderP['price_month']-$orderP['price_free']-$orderP['price_give']-$orderP['price_account']-$orderP['price_wechat'];//欠款金额0--100=100-100
            $orderP['longitude'] = $longitude;
            $orderP['latitude'] = $latitude;
            $orderDb->saveOrderData($orderId,$orderP);
            $passOrder = $orderDb->getOrderFind([['send_id','=',$orderData['send_id']],['stateenum','=',4],['iscancel','=',0]]);
            if(empty($passOrder)){
                $orderStateData = $orderDb->getOrderFind([['order_id','=',$orderId]],'stateenum');
                $sendStateData = $sendDb->getSendFind([['send_id','=',$orderData['send_id']]],'stateenum');
                if($orderStateData['stateenum']>8 || $sendStateData['stateenum']>8){
                    $isPassStatus = 1;
                }
                if($isPassStatus == 1){
                    $orderSaveDatas['stateenum'] = 16;
                    $sendSaveDatas['stateenum'] = 16;
                    $workerSaveDatas['sendstateenum'] = 16;
                }else{
                    $orderSaveDatas['stateenum'] = 8;
                    $workerSaveDatas['sendstateenum'] = 8;
                    $sendSaveDatas['stateenum'] = 8;
                    if($sendData['date_order_ok_first']){
                        $sendSaveDatas['date_order_ok_last'] = date("Y-m-d H:i:s",time());
                    }else{
                        $sendSaveDatas['date_order_ok_first'] = date("Y-m-d H:i:s",time());
                    }
                }
                $orderDb->saveOrderData($orderId,$orderSaveDatas);
                $sendDb->saveSendData($orderData['send_id'],$sendSaveDatas);
                $workerDb->updateByWorkerId($this->workerId,$workerSaveDatas);
            }

            //添加客户经纬度
            $customerData['customer_id'] = $orderData['customer_id'];
            $customerData['address'] = $orderData['customer_address'];
            $customerData['create_time'] = date("Y-m-d H:i:s",time());
            $customerData['worker_id_add'] = $this->workerId;
            $customerData['lon'] = $longitude;
            $customerData['lat'] = $latitude;
            if($orderData['customer_address_id']){
                $customerAddressDb->updateByCustomerAddressId($orderData['customer_address_id'],$customerData);
            }else{
                $customerAddressDb->addNormalCustomerAddress($customerData);
            }
            // 提交事务
            Db::commit();
            return show(config("status.success"),"操作成功",['success'=>200]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return show(config("status.error"),$e->getMessage());
        }
    }
}
